package com.livestocksolutions.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileFilter;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

import com.livestocksolutions.LivestockSolutions;

/**
 * Show the progress of the Importing data activity to the user.
 * @author Ken DeVries
 * @author Adam Parr
 * 
 * @.modified 22 October 2012
 */
public class ImportWeightDataFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JProgressBar importProgress = new JProgressBar();
	private final JButton closeWindow = new JButton("Close");
	private final JLabel importLabel = new JLabel("Import Data...");
	private boolean running=true;

	/**
	 * Constructor.
	 */
	public ImportWeightDataFrame(){
		setTitle("Import Data");
		setIconImage(MainFrame.getImage(MainFrame.APP_ICON));
		importLabel.setHorizontalAlignment(SwingConstants.CENTER);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		add(importLabel);
		add(importProgress, BorderLayout.SOUTH);
		setSize(300,100);
		setLocationRelativeTo(null);
		setMinimumSize(new Dimension(300,100));
		importProgress.setIndeterminate(true);
		importProgress.setValue(0);
		setVisible(true);
	}
	
	/**
	 * Start the Backup action and display progress and any errors to the user.
	 */
	public void start(){
		running=true;
		final JFileChooser jfc = new JFileChooser();
		jfc.setAcceptAllFileFilterUsed(false);
		jfc.setFileFilter(new FileFilter(){
			@Override
			public boolean accept(File pathname) {
				if(pathname.isDirectory() || pathname.getName().toLowerCase().endsWith(".csv")){
					return true;
				}
				return false;
			}

			@Override
			public String getDescription() {
				return null;
			}
		});
		jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		jfc.setDialogTitle("Please select a CSV file to import from");
		if(jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
			new Thread(){
				@Override
				public void run(){
					String message="";
					while(running){
						try {
							sleep(50);
						} catch (InterruptedException e) {}
						int percent = LivestockSolutions.getProgress();
						percent=percent<0||percent>100?100:percent;
						message=LivestockSolutions.getMessage();
						importLabel.setText("<html><body>Importing... ("+percent+"%) "+message+"</body></html>");
						importProgress.setValue(LivestockSolutions.getProgress());
					}
					setVisible(false);
					String tmessage=LivestockSolutions.getMessage();
					message=tmessage==""?message:tmessage;
					importLabel.setText("<html><body>"+message+"</body></html>");
					ImportWeightDataFrame.this.remove(importProgress);
					JPanel closePanel = new JPanel();
					JButton information = new JButton("View Log");
					closePanel.add(information, BorderLayout.WEST);
					closePanel.add(closeWindow, BorderLayout.EAST);
					add(closePanel, BorderLayout.SOUTH);
					setVisible(true);

					closeWindow.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e){
							interrupt();
							dispose();
						}
					});
					
					information.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e){
							JFrame log = new JFrame();
							JScrollPane jsp = new JScrollPane();
							JTextPane f=new JTextPane();
							f.setText(LivestockSolutions.getLog());
							f.setEditable(false);
							f.setBackground(null);
							f.setBorder(null);

							jsp.setViewportView(f);
							log.add(jsp);
							log.setSize(500,400);
							log.setLocationRelativeTo(null);
							log.setMinimumSize(new Dimension(50,50));
							log.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
							log.setVisible(true);
						}
					});
				}
			}.start();
			importProgress.setIndeterminate(false);
			new Thread(){
				@Override
				public void run(){
					LivestockSolutions.importData(jfc.getSelectedFile().getAbsolutePath());
					running = false;
				}
			}.start();
		}else{
			dispose();
		}
	}
}
